package com.itheima.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Banner;
import com.itheima.entity.Movies;
import com.itheima.mapper.BannerMapper;
import com.itheima.mapper.MoviesMapper;
import com.itheima.service.MovieService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MoviesMapper moviesMapper;

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public PageInfo<Movies> findByPage(int pageNum, int pageSize, Integer cid, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        keyword = (keyword != null ? keyword.trim() : keyword);
        List<Movies> list = moviesMapper.findByPage(cid, keyword);
        return new PageInfo<>(list);
    }

    @Override
    public Movies findById(Integer id) {
        return moviesMapper.findById(id);
    }

    @Override
    public List<Movies> findAll() {
        return moviesMapper.findAll();
    }

    @Override
    public void save(Movies movies) {
        moviesMapper.save(movies);
    }

    @Override
    public void update(Movies movies) {
        moviesMapper.update(movies);
    }

    @Override
    public void deleteById(Integer id) {
        moviesMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        if (ArrayUtil.isNotEmpty(ids)) {
            moviesMapper.deleteByIds(ids);
        }
    }

    @Override
    public void setMovieToBanner(Integer id) {
        Movies movies = moviesMapper.findById(id);
        Banner banner = new Banner();
        BeanUtils.copyProperties(movies, banner);
        bannerMapper.save(banner);
    }
}
